package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;



import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.ArrayUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    //编辑查看员工信息第种方式 数据回显
    @GetMapping("/{id}")
   /* public Result getById(@PathVariable("id") Integer id){
        Emp emp = empService.getEmpWithExprById(id);
        return Result.success(emp);
    }*/

    //编辑查看员工信息第二种方式  数据回显
    public Result getById(@PathVariable("id") Integer id){
        Emp emp = empService.getEmpById(id);
        return Result.success(emp);
    }
    //修改保存员工数据
    @PutMapping
    public Result updateEmp(@RequestBody Emp emp){
        empService.updateEmp(emp);
        return Result.success();
    }


    @DeleteMapping
    public Result deleteEmp(@RequestParam("ids") List<Integer> ids) {

        log.info("接受到的参数ids:" + ArrayUtil.toString(ids));
        empService.deleteEmp(ids);

        return Result.success();
    }

    @GetMapping
    public Result selectEmp(
           /* @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String name,
            Integer gender,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end*/

            EmpQueryParam empQueryParam

    ) {

        PageResult<Emp> pageResult = empService.selectEmp(empQueryParam);
        return Result.success(pageResult);
    }

    @PostMapping(name = "新增员工以及员工工作经历")
    public Result saveEmp(@RequestBody Emp emp) {
        empService.saveEmp(emp);
        return Result.success();
    }

    //查询全部员工信息
    @GetMapping("/list")
    public Result selectAllEmp(){
        List<Emp> empList = empService.selectAllEmp();
        return Result.success(empList);
    }

}
